package com.prs.crm.action.pm;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.crm.CustomerLevel;
import com.prs.crm.domain.pm.PmType;
import com.prs.crm.service.pm.PmTypeService;
import com.prs.crm.service.sys.LogService;

public class PmTypeAction extends BaseAction {
	private PmType pmType;
	private PmTypeService pmTypeService;
	private PaginationSupport pagination;
	private Integer[] ids;
	private LogService logService;

	@Action("listPmType")
	public String listPmType() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				PmType.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getPmTypeService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editPmType")
	public String editPmType() {
		return SUCCESS;
	}

	@Action(value = "submitPmType", results = {
			@Result(name = "success", type = "redirect", location = "listPmType"),
			@Result(name = "error", type = "chain", location = "editPmType") })
	public String submitPmType() {
		if (pmType.getName() == null || pmType.getName().trim().equals("")) {
			this.addActionError("请正确填写项目类型名称");
			return ERROR;
		}
		if (this.getLogService().getSearchName(PmType.class, pmType.getName())) {
			this.addActionError("名称已经存在");
			return ERROR;
		}
		this.getPmTypeService().save(pmType);
		return SUCCESS;
	}

	@Action(value = "removePmType", results = {
			@Result(name = "success", type = "redirect", location = "listPmType"),
			@Result(name = "error", type = "chain", location = "listPmType") })
	public String removePmType() {
		if (ids == null) {
			this.addActionError("请选择要删除的项目类型");
			return ERROR;
		}
		for (Integer id : ids) {
			PmType type = this.getPmTypeService().get(id);
			if (type.getProjects().size() != 0) {
				this.addActionError("已有项目引用，不能删除");
				return ERROR;
			}
		}
		this.getPmTypeService().batchRemove(ids);
		return SUCCESS;
	}

	public PmType getPmType() {
		return pmType;
	}

	public void setPmType(PmType pmType) {
		this.pmType = pmType;
	}

	public PmTypeService getPmTypeService() {
		return pmTypeService;
	}

	public void setPmTypeService(PmTypeService pmTypeService) {
		this.pmTypeService = pmTypeService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}
}
